package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApAuthor;
import com.heima.common.Threadlocals.UserThreadLocalUtil;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.enums.BehaviorEnum;
import com.heima.common.enums.UserEnum;
import com.heima.feign.article.ArticleFeign;
import com.heima.user.dto.FollowBehaviorDto;
import com.heima.user.dto.UserRelationDto;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserFollow;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.service.IApUserFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP用户关注信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-06
 */
@Service
public class ApUserFollowServiceImpl extends ServiceImpl<ApUserFollowMapper, ApUserFollow> implements IApUserFollowService {

    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    /**
     * 关注行为话题
     */
    @Value("${topic.followBehaviorTopic}")
    private String followBehaviorTopic;


    /**
     * 关注作者
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(UserRelationDto dto) {
        User user = UserThreadLocalUtil.get();

        //1.根据作者ID远程调用文章微服务得到作者信息
        ResponseResult<ApAuthor> responseResult = articleFeign.getAuthorById(dto.getAuthorId());
        if (responseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            ApAuthor apAuthor = responseResult.getData();

            //2.构建关注实体对象 封装参数；进行保存
            //2.1 先查询是否有关注记录
            LambdaQueryWrapper<ApUserFollow> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApUserFollow::getUserId, user.getUserId());
            queryWrapper.eq(ApUserFollow::getFollowId, apAuthor.getUserId());
            ApUserFollow one = this.getOne(queryWrapper);
            if (one != null) {
                //已有关注记录 判断操作类型
                if (dto.getOperation().equals(1)) {
                    this.remove(queryWrapper);
                }
            }else{
                //2.2 如果没有关注记录 则新增
                ApUserFollow apUserFollow = new ApUserFollow();
                apUserFollow.setUserId(user.getUserId());
                apUserFollow.setUserName(user.getUserName());

                apUserFollow.setFollowId(apAuthor.getUserId());
                apUserFollow.setFollowName(apAuthor.getName());

                apUserFollow.setLevel(UserEnum.USER_FOLLOW_LEVEL_ONCE.getVal());
                this.save(apUserFollow);
            }

            //3.发送消息到Kafka
            //3.1 构建发送关注行为实体类
            FollowBehaviorDto followBehaviorDto = new FollowBehaviorDto();
            followBehaviorDto.setUserId(user.getUserId());
            followBehaviorDto.setFollowId(apAuthor.getUserId());
            followBehaviorDto.setOperation(dto.getOperation());
            followBehaviorDto.setEquipmentId(dto.getEquipmentId());
            //3.2 发送关注行为消息到kafka中
            kafkaTemplate.send(followBehaviorTopic, JSON.toJSONString(followBehaviorDto));
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
}
